# FadeTransition - Fade out old screen and fade in the new screen
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import clutter
from frontend.gui.transitions.transition import Transition

class FadeTransition(Transition):
    """
    FadeTransition 
    
    Fade out all components from the "from_screen" and fade in all components
    of the "to_screen". This class implements simple crossfade effect for
    screen changes.
    """
    
    def forwardEffect(self, from_screen, to_screen):   
        """ 
        Fade out all components from the "from_screen" and fade in all components
        of the "to_screen".
        @param from_screen: Screen object (Currently displayed screen)
        @param to_screen: Screen object (Screen that is displayed after transition)
        """
        # Initialize to_screen for animation
        to_screen.get_group().set_opacity(0x00)
        to_screen.get_group().show()
        
        # Fade out current screen
        if from_screen is not None:
            self.fade_out = clutter.Timeline(20, 60)
            self.alpha_out = clutter.Alpha(self.fade_out, clutter.smoothstep_inc_func)
            self.out_behaviour = clutter.BehaviourOpacity(0xff, 0x00, self.alpha_out)
            self.out_behaviour.apply(from_screen.get_group())
            print "Direction: "+ str(self.direction)
            if self.direction == self.BACKWARD:
                self.direction = self.FORWARD # Reset value
                self.fade_out.connect('completed', self._remove_from_stage_callback, from_screen)
        
        # Fade in timeline
        self.fade_in = clutter.Timeline(20, 60)
        self.alpha_in = clutter.Alpha(self.fade_in, clutter.smoothstep_inc_func)
        self.in_behaviour = clutter.BehaviourOpacity(0x00, 0xff, self.alpha_in)
        self.in_behaviour.apply(to_screen.get_group())
        
        # Start transition animation
        if from_screen is not None:
            self.fade_out.start()
        self.fade_in.start()
    
    def backwardEffect(self, from_screen, to_screen):
        """ 
        Fade out all components from the "from_screen" and fade in all components
        of the "to_screen".
        @param from_screen: Screen object (Currently displayed screen)
        @param to_screen: Screen object (Screen that is displayed after transition)
        """
        self.direction = self.BACKWARD
        # Same effect. Direction doesn't make any difference
        self.forwardEffect(from_screen, to_screen)
        
    def _remove_from_stage_callback(self, timeline, screen):
        """
        Callback for fadeout timeline. This method is called when transition
        direction is BACKWARDS. This method removes old screen from stage.
        """
        self.stage.remove(screen.get_group())
        